package util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;

import servicios.OrdenCompraCCAcepDTO;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.basic.DateConverter;
import com.thoughtworks.xstream.io.xml.DomDriver;

import dto.ItemOrdenCompraCCDTO;
import dto.MateriaPrimaAPedirDTO;
import dto.OrdenCompraCCDTO;

import entities.ListaDePrecios;
import entities.Rodamiento;

public class XMLParser {
	private static XStream xs = null;
	
	 private static XMLParser instance; 
	 
	  public static synchronized XMLParser getInstance() {
	        if(instance==null){
	                 instance = new XMLParser();
	        }
	        return instance;
	    }

	private XMLParser() {
		xs = new XStream(new DomDriver());
		
		/** Materia Prima **/
		xs.alias("lista", java.util.List.class);
		xs.alias("materiaPrimaAPedirVO", MateriaPrimaAPedirDTO.class);
		xs.alias("Rodamiento", Rodamiento.class);
		
		/** Lista De Precios **/
		xs.alias("ListaDePrecios",ListaDePrecios.class);
		xs.omitField(Rodamiento.class, "listaMateriasPrima");
		xs.omitField(Rodamiento.class, "medida");
		xs.omitField(Rodamiento.class, "caracteristica");
		xs.omitField(Rodamiento.class, "marca");
		xs.omitField(Rodamiento.class, "origen");
		xs.omitField(Rodamiento.class, "tipo");
		xs.omitField(Rodamiento.class, "cod_propietario");
		xs.omitField(Rodamiento.class, "proveedor");
		xs.addDefaultImplementation(java.sql.Date.class,java.util.Date.class); 
		xs.addDefaultImplementation(java.sql.Timestamp.class,java.util.Date.class); 
		xs.addDefaultImplementation(java.sql.Time.class,java.util.Date.class); 
		xs.registerConverter(new DateConverter("yyyyMMdd",new String[12]));
		
		/** OrdenCompraCCAcepVO **/
		xs.alias("OrdenCompraCCAcepVO",OrdenCompraCCAcepDTO.class);
		
		/** OrdenCompraCCVO **/
		xs.alias("OrdenCompraCC",OrdenCompraCCDTO.class);
		xs.alias("ItemOrdenCompraCC",ItemOrdenCompraCCDTO.class);
		
	}

	public synchronized String parsear(Object o) {
		return xs.toXML(o);
	}

	public static synchronized Object parsearObjeto(File f)
			throws FileNotFoundException {
		return xs.fromXML(new FileInputStream(f));
	}

	public synchronized Object parseObject(String f) {
		return xs.fromXML(f);
	}

	public synchronized void parsearYguardarArchivo(Object o, File f)
			throws IOException {
		FileOutputStream outputStream = new FileOutputStream(f);
		Writer writer = new OutputStreamWriter(outputStream, "UTF-8");
		writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
		xs.toXML(o, writer);
	}

	public void generateXmlAndSave(Object xmlObject) {
		String path = "C://ArchivosXML//";
		path += xmlObject.getClass().getSimpleName() + "-"
				+ new SimpleDateFormat("yyyyMMdd-hhss").format(new Date())
				+ ".xml";
		try {
			parsearYguardarArchivo(xmlObject, new File(path));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
